package com.wiz.weixin.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.wiz.weixin.manager.WxAccessManager;
import com.wiz.weixin.service.ModelMessageService;
import com.wiz.weixin.util.TencentUtil;
import com.wiz.weixin.vo.message.AmountMessageVO;
import com.wiz.weixin.vo.message.ChargeMessageVO;
import com.wiz.weixin.vo.message.ExchangePointMessageVO;
import com.wiz.weixin.vo.message.MessageNode;
import com.wiz.weixin.vo.message.ModelMessageVO;

@Service("modelMessageService")
public class ModelMessageServiceImpl implements ModelMessageService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private Gson gson;
	@Resource
	private WxAccessManager wxAccessManager;
	
	private final String send_msg_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";

	@Override
	public String sendConsumpMessage(String openId,String hqName,String stationName,
			String amount,String point,String blanceAmount,String balancePoint,String paidTypeName,
			String createdTime) {
		String requestUrl = String.format(send_msg_url, WxAccessManager.getTencent_wx_token());
		String first = "尊敬的客户：\n您的【"+hqName+"】的账户发生消费";
		MessageNode fn = new MessageNode(first,"#000000");
		MessageNode mn1 = new MessageNode(amount+"元","#000000");
		MessageNode mn2 = new MessageNode(stationName,"#000000");
		MessageNode mn3 = new MessageNode(point,"#000000");
		MessageNode mn4 = new MessageNode(balancePoint,"#000000");
		MessageNode mn5 = new MessageNode(blanceAmount+"元","#000000");
//		String remark = "查看账户详情，请<a href='"+
//				"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5d7213f7ae8e6fdf&redirect_uri=http://wx.wiz-tech.com.cn/member/listAccount&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
//				+"'>点击</a>>";
		String remark = "支付方式 ："+paidTypeName + "\n";
		remark += "消费时间 ："+createdTime;
		MessageNode rm = new MessageNode(remark,"#000000");
		AmountMessageVO amountData = new AmountMessageVO();
		amountData.setFirst(fn);
		amountData.setKeyword1(mn1);
		amountData.setKeyword2(mn2);
		amountData.setKeyword3(mn3);
		amountData.setKeyword4(mn4);
		amountData.setKeyword5(mn5);
		amountData.setRemark(rm);
		ModelMessageVO messageVO= new ModelMessageVO(openId,"yv_mj0Pi-iuaBAicUEOnXGDlsoZGbAG81dbabu9oQ2g");
		messageVO.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5d7213f7ae8e6fdf&redirect_uri=http://wx.wiz-tech.com.cn/member/listAccount&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
		messageVO.setData(amountData);
		String response =TencentUtil.httpsRequest(requestUrl, "POST", gson.toJson(messageVO));
		log.info(openId+" sendConsumpMessage.response="+response);
		if(response.indexOf("40001") >= 0){
			wxAccessManager.resetTempTask();
		}
		return response;
	}

	public String sendChargeMessage(String openId,String hqName,String stationName,
			String amount,String blanceAmount,String giftAmount,String createdTime) {
		String requestUrl = String.format(send_msg_url, WxAccessManager.getTencent_wx_token());
		String first = "尊敬的客户：\n您的【"+hqName+"】的账户有新的充值";
		MessageNode fn = new MessageNode(first,"#000000");
		MessageNode mn1 = new MessageNode(amount+"元","#000000");
		MessageNode mn2 = new MessageNode(giftAmount+"元","#000000");
		MessageNode mn3 = new MessageNode(stationName,"#000000");
		MessageNode mn4 = new MessageNode(blanceAmount+"元","#000000");
//		String remark = "查看账户详情，请<a href='"+
//				"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5d7213f7ae8e6fdf&redirect_uri=http://wx.wiz-tech.com.cn/member/listAccount&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
//				+"'>点击</a>>";
		String remark = "充值时间 ："+createdTime;
		MessageNode rm = new MessageNode(remark,"#000000");
		ChargeMessageVO amountData = new ChargeMessageVO();
		amountData.setFirst(fn);
		amountData.setKeyword1(mn1);
		amountData.setKeyword2(mn2);
		amountData.setKeyword3(mn3);
		amountData.setKeyword4(mn4);
		amountData.setRemark(rm);
		ModelMessageVO messageVO= new ModelMessageVO(openId,"zZg0UFliHO2Ab-6NKdZ2HNmvFvzs5YbruB7V0V4RUck");
		messageVO.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5d7213f7ae8e6fdf&redirect_uri=http://wx.wiz-tech.com.cn/member/listAccount&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
		messageVO.setData(amountData);
		String response =TencentUtil.httpsRequest(requestUrl, "POST", gson.toJson(messageVO));
		log.info(openId+" sendChargeMessage.response="+response);
		if(response.indexOf("40001") >= 0){
			wxAccessManager.resetTempTask();
		}
		return response;
	}
	
	public static void main(String[] args) {
		String s = "{\"errcode\":40001,\"errmsg\":\"invalid credential, access_token is invalid or not latest hint: [z0638vr29]\"}";
		System.out.println(s.indexOf("40001"));
	}

	@Override
	public String sendPointMessage(String openId, String hqName, String stationName, String point, String blancePoint,
			String createdTime) {
		String requestUrl = String.format(send_msg_url, WxAccessManager.getTencent_wx_token());
		String first = "尊敬的客户：\n您的【"+hqName+"】的账户有新的积分变化下";
		MessageNode fn = new MessageNode(first,"#000000");
		MessageNode mn1 = new MessageNode("兑换礼品","#000000");
		MessageNode mn2 = new MessageNode(stationName,"#000000");
		MessageNode mn3 = new MessageNode(point,"#000000");
		MessageNode mn4 = new MessageNode(blancePoint,"#000000");
		String remark = "消费时间 ："+createdTime;
		MessageNode rm = new MessageNode(remark,"#000000");
		ExchangePointMessageVO amountData = new ExchangePointMessageVO();
		amountData.setFirst(fn);
		amountData.setKeyword1(mn1);
		amountData.setKeyword2(mn2);
		amountData.setKeyword3(mn3);
		amountData.setKeyword4(mn4);
		amountData.setRemark(rm);
		ModelMessageVO messageVO= new ModelMessageVO(openId,"NGt3K4YKvglFtomG9RYj_mlJzGZ3FhURjbI_dxNHJaA");
		messageVO.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5d7213f7ae8e6fdf&redirect_uri=http://wx.wiz-tech.com.cn/member/listAccount&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
		messageVO.setData(amountData);
		String response =TencentUtil.httpsRequest(requestUrl, "POST", gson.toJson(messageVO));
		log.info(openId+" sendPointMessage.response="+response);
		if(response.indexOf("40001") >= 0){
			wxAccessManager.resetTempTask();
		}
		return response;
	}
}
